#!/bin/bash
# This script is released under the GNU General Public License 3.0
# Check the COPYING file included with this distribution

# FSspec_add()
# user GUI to add a new FSspec
#
# parameters (required):
#  CONFIG_LIST: One string with 0 or more items of defined FSspec
#	Already configured partitions Need this to trigger setup of root/swap partition
#
# returns 0 on success
# returns $ERROR_CANCEL=64 on user cancel
# anything else is a real error
# reason: show_dialog() needs a way to exit "Cancel"
#
# writes menus and noise to STDERR
#
# prints result to STDOUT

# location of other scripts to source
readonly SHAREDIR="$(dirname ${0})" || exit $?

# source partitioning commons
source "${SHAREDIR}"/partition_common.sh || exit $?

#####################
## begin execution ##

# check input
check_num_args "$(basename $0)" 1 $# || exit $?
CONFIG_LIST="${1}"
HAS_ROOT=0
HAS_SWAP=0
RETSUB=

# properties of FSspec
PARTITION=
MOUNTPOINT=
FILESYSTEM=

# check for root/swap partition
"${SHAREDIR}"/FSspec list_haskeyvalue "${CONFIG_LIST}" 'mountpoint' '/' && HAS_ROOT=1
"${SHAREDIR}"/FSspec list_haskeyvalue "${CONFIG_LIST}" 'filesystem' 'swap' && HAS_SWAP=1
# init root/swap handling
if [ "${HAS_ROOT}" -ne 1 ]; then
	show_dialog --msgbox "You can now define the root partition (mountpoint '/')." 0 0
	MOUNTPOINT='/'
	# assume ext4 as default
	FILESYSTEM='ext4'
fi
# ask user for partition
PARTITION="$(partition_selectpartition '_')" || exit $?
# check if partition is already configured
if "${SHAREDIR}"/FSspec list_haskeyvalue "${CONFIG_LIST}" 'partition' "${PARTITION}"; then
	show_dialog --msgbox "ERROR: Partition ${PARTITION} is already configured!" 0 0
	exit "${ERROR_CANCEL}"
fi
if [ -z "${MOUNTPOINT}" ]; then
	show_dialog --yesno "Do you want to use ${PARTITION} as swap partition?" 0 0
	RETSUB=$?
	if [ "${RETSUB}" -eq 0 ]; then
		FILESYSTEM='swap'
	else
		# assume ext4 as default
		FILESYSTEM='ext4'
	fi
fi
if [ "${FILESYSTEM}" != 'swap' ]; then
	if [ -z "${MOUNTPOINT}" ]; then
		# ask for mountpoint
		MOUNTPOINT="$(show_dialog --inputbox "Enter the mountpoint for ${PARTITION}." 0 0 "${MOUNTPOINT}")" || exit $?
		# trim result
		MOUNTPOINT="$(echo "${MOUNTPOINT}" | awk '{gsub(/^ +| +$/,"")} {print $0}')" || exit $?
		# validate
		if [ -z "${MOUNTPOINT}" ]; then
			show_dialog --msgbox "ERROR: You have entered an empty mountpoint!" 0 0
			exit "${ERROR_CANCEL}"
		elif [ "${MOUNTPOINT:0:1}" != '/' ]; then
			show_dialog --msgbox "ERROR: A valid mountpoint begins with '/'!" 0 0
			exit "${ERROR_CANCEL}"
		# check if mountpoint is already configured
		elif "${SHAREDIR}"/FSspec list_haskeyvalue "${CONFIG_LIST}" 'mountpoint' "${MOUNTPOINT}"; then
			show_dialog --msgbox "ERROR: Mountpoint ${MOUNTPOINT} is already configured!" 0 0
			exit "${ERROR_CANCEL}"
		fi
		# set ext2 for /boot
		[ "${MOUNTPOINT}" = '/boot' ] && FILESYSTEM='ext2'
	fi
fi
# everything ok, create new FSspec string
# print result to STDOUT and exit 0
CONFIG_ITEM="$("${SHAREDIR}"/FSspec create \
	"${PARTITION}" \
	"${MOUNTPOINT}" \
	"${FILESYSTEM}" \
	"" \
	"1")" || exit $?
echo "${CONFIG_ITEM}"
exit 0
